package com.zjitc.controller;

import com.zjitc.pojo.Book;
import com.zjitc.pojo.User;
import com.zjitc.service.BookService;
import com.zjitc.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.List;

/**
 * 用户管理Controller
 *
 * @author Admin
 */
@Controller
@RequestMapping("/users")
public class UserController {

    @Autowired
    private UserService userService;

    @Autowired
    private BookService bookService;

    @RequestMapping("/index")
    public ModelAndView index(HttpServletRequest request) {

        HttpSession session = request.getSession();
        User user = (User) session.getAttribute("user");
        ModelAndView mv = new ModelAndView();
        if (user != null) {
            User byUserName = userService.findByUserName(user.getUsername());
            switch (byUserName.getRole()) {
                case 0:
                    List<BAH> allBAH = userService.findAllBAH(byUserName.getId());
                    mv.addObject("BAHList", allBAH);
                    mv.setViewName("reader");
                    break;
                case 1:
                    List<Book> all = bookService.findAll();
                    mv.addObject("bookList", all);
                    mv.setViewName("staff");
                    break;
                case 2:
                    List<User> users = userService.getUsers(0);
                    List<Book> books = bookService.findAll();
                    mv.addObject("userList", users);
                    mv.addObject("bookList", books);
                    mv.setViewName("admin");
            }
        }
        return mv;
    }

    @RequestMapping("/save")
    public ModelAndView save(User user) {
        userService.insert(user);
        ModelAndView mv = new ModelAndView();
        List<User> users = userService.getUsers(LoginController.role);
        mv.addObject("userList", users);
        mv.setViewName("redirect:/users/index");
        return mv;
    }

    @RequestMapping("/delete")
    public ModelAndView delete(int index) {
        boolean delete = userService.delete(index);
        ModelAndView mv = new ModelAndView();
        List<User> users = userService.getUsers(LoginController.role);
        if (delete) {
            mv.addObject("userList", users);
            mv.setViewName("redirect:/users/index");
            return mv;
        }
        return mv;
    }

    @RequestMapping("/update")
    public String update(User user) {
        userService.update(user);
        return "redirect:/users/index";
    }

    @RequestMapping("/updateEntry")
    public ModelAndView updateEntry(int id) {
        User user = userService.findOne(id);
        ModelAndView mv = new ModelAndView();
        mv.addObject("user", user);
        mv.setViewName("update");
        return mv;
    }

    @RequestMapping("/insertEntry")
    public String insertEntry() {
        return "insert";
    }

    public static class BAH {
        private Integer id;
        private Integer userId;
        private String position;
        private String name;
        private Date lastBt;
        private Integer status;

        public Integer getId() {
            return id;
        }

        public void setId(Integer id) {
            this.id = id;
        }

        public Integer getUserId() {
            return userId;
        }

        public void setUserId(Integer userId) {
            this.userId = userId;
        }

        public String getName() {
            return name;
        }

        public void setName(String name) {
            this.name = name;
        }

        public Date getLastBt() {
            return lastBt;
        }

        public void setLastBt(Date lastBt) {
            this.lastBt = lastBt;
        }

        public Integer getStatus() {
            return status;
        }

        public void setStatus(Integer status) {
            this.status = status;
        }

        public String getPosition() {
            return position;
        }

        public void setPosition(String position) {
            this.position = position;
        }
    }
}
